//
//  WXApiManager.m
//  SDKSample
//
//  Created by Jeason on 16/07/2015.
//
//

#import "WXApiManager.h"
#import "SaveResultManager.h"

@implementation WXApiManager

#pragma mark - LifeCycle
+(instancetype)sharedManager {
    static dispatch_once_t onceToken;
    static WXApiManager *instance;
    dispatch_once(&onceToken, ^{
        instance = [[WXApiManager alloc] init];
    });
    return instance;
}

- (void)dealloc {
    self.delegate = nil;
}

#pragma mark - WXApiDelegate
- (void)onResp:(BaseResp *)resp {
    if ([resp isKindOfClass:[SendMessageToWXResp class]]) {
        SendMessageToWXResp *sendResp = (SendMessageToWXResp *)resp;
         //使用UIAlertView 显示回调信息
        NSString *str = [NSString stringWithFormat:@"%d",sendResp.errCode];
        NSLog(@"===%@",str);
        switch (sendResp.errCode) {
            case WXSuccess:{
                [SaveResultManager share].weixinShareResult = @"微信分享成功";
                [[SaveResultManager share] saveWXShareResult];
            }
                break;
            case WXErrCodeUserCancel:{
                [SaveResultManager share].weixinShareResult = @"微信分享取消";
                [[SaveResultManager share] saveWXShareResult];
            }
                break;
            case WXErrCodeSentFail:{
                [SaveResultManager share].weixinShareResult = @"微信分享失败";
                [[SaveResultManager share] saveWXShareResult];
            }
                break;
            case WXErrCodeUnsupport:{
                [SaveResultManager share].weixinShareResult = @"微信不支持";
                [[SaveResultManager share] saveWXShareResult];
            }
                break;
            default:{
                [SaveResultManager share].weixinShareResult = @"分享失败";
                [[SaveResultManager share] saveWXShareResult];
            }
                break;
        }
    
    } else if ([resp isKindOfClass:[SendAuthResp class]]) {
        if (_delegate
            && [_delegate respondsToSelector:@selector(managerDidRecvAuthResponse:)]) {
            SendAuthResp *authResp = (SendAuthResp *)resp;
            [_delegate managerDidRecvAuthResponse:authResp];
        }
    } else if ([resp isKindOfClass:[AddCardToWXCardPackageResp class]]) {
        if (_delegate
            && [_delegate respondsToSelector:@selector(managerDidRecvAddCardResponse:)]) {
            AddCardToWXCardPackageResp *addCardResp = (AddCardToWXCardPackageResp *)resp;
            [_delegate managerDidRecvAddCardResponse:addCardResp];
        }
    }
        else if([resp isKindOfClass:[PayResp class]]){
        //支付返回结果，实际支付结果需要去微信服务器端查询
        NSString *WXresult = [NSString string];
        switch (resp.errCode) {
            case WXSuccess:{
                WXresult = @"支付成功";
                [SaveResultManager share].weixinPayResult = @"支付成功";
                [[SaveResultManager share] saveWX];
            }
                break;
            case WXErrCodeCommon:{
                WXresult = @"支付失败";
                [SaveResultManager share].weixinPayResult = @"支付失败";
                [[SaveResultManager share] saveWX];
            }
                break;
            case WXErrCodeUserCancel:{
                WXresult = @"取消支付";
                [SaveResultManager share].weixinPayResult = @"取消支付";
                [[SaveResultManager share] saveWX];
            }
                break;
            case WXErrCodeSentFail:{
                WXresult = @"发送失败";
                [SaveResultManager share].weixinPayResult = @"发送失败";
                [[SaveResultManager share] saveWX];
            }
                break;
            case WXErrCodeUnsupport:{
                WXresult = @"微信不支持";
                [SaveResultManager share].weixinPayResult = @"微信不支持";
                [[SaveResultManager share] saveWX];
                
            }
                break;
            case WXErrCodeAuthDeny:{
                WXresult = @"授权失败";
                [SaveResultManager share].weixinPayResult = @"授权失败";
                [[SaveResultManager share] saveWX];
            }
                break;
            default:
                break;
        }
    }

}




- (void)onReq:(BaseReq *)req {
    if ([req isKindOfClass:[GetMessageFromWXReq class]]) {
        if (_delegate
            && [_delegate respondsToSelector:@selector(managerDidRecvGetMessageReq:)]) {
            GetMessageFromWXReq *getMessageReq = (GetMessageFromWXReq *)req;
            [_delegate managerDidRecvGetMessageReq:getMessageReq];
        }
    } else if ([req isKindOfClass:[ShowMessageFromWXReq class]]) {
        if (_delegate
            && [_delegate respondsToSelector:@selector(managerDidRecvShowMessageReq:)]) {
            ShowMessageFromWXReq *showMessageReq = (ShowMessageFromWXReq *)req;
            [_delegate managerDidRecvShowMessageReq:showMessageReq];
        }
    } else if ([req isKindOfClass:[LaunchFromWXReq class]]) {
        if (_delegate
            && [_delegate respondsToSelector:@selector(managerDidRecvLaunchFromWXReq:)]) {
            LaunchFromWXReq *launchReq = (LaunchFromWXReq *)req;
            [_delegate managerDidRecvLaunchFromWXReq:launchReq];
        }
    }
}

@end
